package com.google.code.fluentjoiner;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringFunctions
{
    public static Function<String, Iterator<String>> regexSearch(final String regex)
    {
        return new Function<String, Iterator<String>>()
        {
            @Override
            public Iterator<String> apply(final String from)
            {
                final List<String> result = Lists.newLinkedList();
                final Pattern pattern = Pattern.compile(regex);
                final Matcher matcher = pattern.matcher(from.toString());
                while (matcher.find())
                {
                    result.add(matcher.group());
                }
                return result.iterator();
            }
        };
    }

    public static Function<String, Iterator<String>> split(final String regex)
    {
        return new Function<String, Iterator<String>>()
        {
            @Override
            public Iterator<String> apply(final String from)
            {
                return Lists.newArrayList(from.split(regex)).iterator();
            }
        };
    }

    public static final Function<String, String> LOWERCASE = new Function<String, String>()
    {
        @Override
        public String apply(final String from)
        {
            return from.toLowerCase();
        }
    };

    public static final Function<String, String> UPPERCASE = new Function<String, String>()
    {
        @Override
        public String apply(final String from)
        {
            return from.toUpperCase();
        }
    };
}